/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.sys.entity;


import com.easy.common.utils.IdGen;
import com.easy.common.utils.StringUtils;
import com.easy.common.utils.excel.annotation.ExcelField;
import com.easy.core.persistence.BaseEntity;
import com.easy.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 * 数据权限Entity
 * @author lgf
 * @version 2017-04-02
 */
public class DataRule extends BaseEntity<DataRule> {
	
	private static final long serialVersionUID = 1L;
	private String id;
	private Integer dataruleId;
	private String menuId;		// 所属菜单
	private String className;   //实体类名
	private String dataruleField;		// 规则字段
	private String dataruleExpress;		// 规则条件
	private String dataruleValue;		// 规则值
	private String sqlSegment;		// 自定义sql
	protected String remarks;	// 备注
	protected String delFlag; 	// 删除标记（0：正常；1：删除；2：审核）
	private String dataruleName;		// 数据规则名称

	
	public DataRule() {
		super();
	}

	public DataRule(Integer dataruleId){
		this.dataruleId= dataruleId;
	}

	public DataRule(String id){
		this.id= id;
	}

	public Integer getDataruleId() {
		return dataruleId;
	}

	public void setDataruleId(Integer dataruleId) {
		this.dataruleId = dataruleId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	@ExcelField(title="所属菜单", align=2, sort=1)
	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	
	@ExcelField(title="数据规则名称", align=2, sort=2)
	public String getDataruleName() {
		return dataruleName;
	}

	public void setDataruleName(String dataruleName) {
		this.dataruleName = dataruleName;
	}
	
	@ExcelField(title="规则字段", align=2, sort=3)
	public String getDataruleField() {
		return dataruleField;
	}

	public void setDataruleField(String dataruleField) {
		this.dataruleField = dataruleField;
	}
	
	@ExcelField(title="规则条件", dictType="t_express", align=2, sort=4)
	public String getDataruleExpress() {
		return dataruleExpress;
	}

	public void setDataruleExpress(String dataruleExpress) {
		this.dataruleExpress = dataruleExpress;
	}
	
	@ExcelField(title="规则值", align=2, sort=5)
	public String getDataruleValue() {
		return dataruleValue;
	}

	public void setDataruleValue(String dataruleValue) {
		this.dataruleValue = dataruleValue;
	}
	
	@ExcelField(title="自定义sql", align=2, sort=6)
	public String getSqlSegment() {
		return sqlSegment;
	}

	public void setSqlSegment(String sqlSegment) {
		this.sqlSegment = sqlSegment;
	}
	
	public String getDataScopeSql(){
		StringBuffer sqlBuffer = new StringBuffer();
		if(StringUtils.isNotBlank(dataruleField)&&StringUtils.isNotBlank(dataruleValue)){
			sqlBuffer.append(" AND " +dataruleField+" "+StringEscapeUtils.unescapeHtml4(dataruleExpress)+" "+dataruleValue+" ");
		}
		if(StringUtils.isNotBlank(sqlSegment)){
			sqlBuffer.append(" AND "+StringEscapeUtils.unescapeHtml4(sqlSegment)+" ");
		}
		
		return sqlBuffer.toString();
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}


	/**
	 * 插入之前执行方法，需要手动调用
	 */
	public void preInsert(){
		this.id = IdGen.uuid();
		User user = UserUtils.getUser();
		//if (user.getUserId()<1){
		//	this.updateBy = user;
		//	this.createBy = user;
		//}
		//this.updateTime = new Date();
		//this.createTime = this.updateTime;
	}

	/**
	 * 更新之前执行方法，需要手动调用
	 */
	public void preUpdate(){
		//User user = UserUtils.getUser();
		//if (user.getUserId()<1){
		//	this.updateBy = user;
		//}
		//this.updateTime = new Date();
	}
}